package com.huiyun.project.im.server.biz.framework;

import cn.hutool.core.lang.Assert;
import com.huiyun.project.im.consts.ImServerConst;
import com.huiyun.project.im.core.service.IImAppService;
import com.huiyun.project.im.core.service.IImUserService;
import com.huiyun.project.im.common.auth.Principal;
import com.huiyun.project.im.common.auth.PrincipalService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * @FileName: PrincipalServiceImpl
 * @Description:
 * @Author linjie
 * @Date 2020/7/184:55 PM
 * @Version 4.0.0
 */
@Component
@AllArgsConstructor
public class PrincipalServiceImpl implements PrincipalService {


    private final IImUserService userService;

    private final IImAppService appService;

    /**
     * 所有的登录uid都要加个前缀
     *
     * @param uid
     * @return
     */
    @Override
    public Principal findPrincipalByUid(String uid) {
        Principal principal = null;
        Assert.isTrue(uid.indexOf(":") != -1, "userId error");
        String uinfo[] = uid.split(":");
        if (uid.startsWith(ImServerConst.APP_STORE_PREFIX)) {
            principal = appService.findPrincipal(uinfo[1]);
        } else {
            principal = userService.findPrincipal(uinfo[1]);
        }
        Assert.notNull(principal, "principal 404");
        return principal;
    }
}
